package railRoad.web.slaves;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import railRoad.Route;
import railRoad.Schedule;
import railRoad.Station;
import railRoad.web.slaves.interfaces.ISlaveServlet;
import railRoad.worker.FactoryRW;

public class StationUpgradeServlet implements ISlaveServlet {

	public void work(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {

		Map<?, ?> parameterMap = req.getParameterMap();

		if (parameterMap.containsKey("name")
				&& parameterMap.containsKey("longitude")
				&& parameterMap.containsKey("altitude")
				&& parameterMap.containsKey("id_station")) {
			
			String stationName = req.getParameterValues("name")[0]; 
			float altitude = Float.parseFloat(req.getParameterValues("altitude")[0]); 
			float longitude = Float.parseFloat(req.getParameterValues("longitude")[0]); 
			int stationId = Integer.getInteger(req.getParameterValues("id_station")[0]); 

			int id_route = (Integer) req.getSession().getAttribute("id_route");

			Schedule schedule = Schedule.getInstance();

			Route route = schedule.getRouteAt(id_route);

			Station station = route.getStationById(stationId);

			station.setStationName(stationName);
			station.setAltitude(altitude);
			station.setLongitude(longitude);

			FactoryRW.update(station);

			req.getSession().removeAttribute("station");
		}//if parameterMap
		resp.sendRedirect("/pages/station_list.do");
	}//work
}//StationUpgradeServlet
